/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Nico
 */
public class Ventas {

    public static ResultSet vieneid;
    public static int id;
    public static ResultSet rs;
    public static ResultSet rs1;
    public static ResultSet rs2;
    public static Statement st;

    public static void Guardar(float venta, String tarjeta, String strTarjeta) throws ClassNotFoundException {
        System.out.println("entro ventas");
        SimpleDateFormat formateador = new SimpleDateFormat("dd 'de' MM 'de' yyyy", new Locale("es_ES"));
        Date fechaDate = new Date();
        String fecha = formateador.format(fechaDate);
        

        Statement st = Controller.Conexion.conectar();



        try {
           
            rs1 = st.executeQuery("SELECT id FROM ventas");
            if (rs1 != null && rs1.next()) {
                
                rs = st.executeQuery("SELECT max(id) FROM ventas"/*
                         * "SELECT id FROM ventas ORDER BY 'id' DESC LIMIT 1"
                         */);
                while (rs.next()) {
                    id = ((Integer) rs.getObject(1)) + 1;
                }
                Class.forName("com.mysql.jdbc.Driver");
                try {
                    Connection conexion = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/ficcion", "root", "4932273");
                    Statement st2 = (Statement) conexion.createStatement();
                    st2.executeUpdate("INSERT INTO ventas (id,cantidad_vendida, dia, es_tarjeta, nombre_tarjeta) VALUES (" + id + ",'" + venta + "','" + fecha + "','" + tarjeta + "','" + strTarjeta + "')");
                   
                } catch (SQLException ex) {
                    Logger.getLogger(Ventas.class.getName()).log(Level.SEVERE, null, ex);
                }

            } else {
                Class.forName("com.mysql.jdbc.Driver");
                try {
                    Connection conexion = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/ficcion", "root", "4932273");
                    Statement st1 = (Statement) conexion.createStatement();
                    st1.executeUpdate("INSERT INTO ventas (id,cantidad_vendida, dia, es_tarjeta, nombre_tarjeta) VALUES (" + 1 + ",'" + venta + "','" + fecha + "','" + tarjeta + "','" + strTarjeta + "')");
                   
                } catch (SQLException ex) {
                    Logger.getLogger(Ventas.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        } catch (Exception e) {
        }



    }
    
    public static ResultSet selectVenta(){
        st = Controller.Conexion.conectar();
        try {
            rs2 = st.executeQuery("SELECT dia,cantidad_vendida,es_tarjeta,nombre_tarjeta from ventas");
            
        } catch (SQLException ex) {
            Logger.getLogger(Ventas.class.getName()).log(Level.SEVERE, null, ex);
        }
       
        return rs2;
    }
    
     public static void borrarDatos(){
        Statement st1 = (Statement) Controller.Conexion.conectar();
        try {
            st1.executeQuery("TRUNCATE ventas");
            
        } catch (SQLException ex) {
            Logger.getLogger(Caja.class.getName()).log(Level.SEVERE, null, ex);
        }
}
}